//
//  DetailViewController.m
//  Ratings
//
//  Created by steven.zhan on 14-9-18.
//  Copyright (c) 2014年 hpxbzz. All rights reserved.
//

#import "DetailViewController.h"

@interface DetailViewController ()

@end

@implementation DetailViewController
{
    UIPopoverController *masterPopoverController;
    UIPopoverController *menuPopoverController;
}

@synthesize toolbar;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    masterPopoverController=nil;
    menuPopoverController=nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return YES;
}

- (void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)pc
{
    barButtonItem.title=@"Master";
    NSMutableArray *items = [[self.toolbar items] mutableCopy];
    [items insertObject:barButtonItem atIndex:0];
    [self.toolbar setItems:items animated:YES];
    masterPopoverController = pc;
}

- (void)splitViewController:(UISplitViewController *)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
    NSMutableArray *items = [[self.toolbar items] mutableCopy];
    [items removeObject:barButtonItem];
    [self.toolbar setItems:items animated:YES];
    masterPopoverController=nil;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"ShowPopover"])
    {
        if (menuPopoverController != nil &&menuPopoverController.popoverVisible) {
            [menuPopoverController dismissPopoverAnimated:NO];
        }
        menuPopoverController=((UIStoryboardPopoverSegue*)segue).popoverController;
        menuPopoverController.delegate=self;
    }
}

- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
    menuPopoverController.delegate=nil;
    menuPopoverController=nil;
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if (menuPopoverController != nil && menuPopoverController.popoverVisible) {
        [menuPopoverController dismissPopoverAnimated:YES];
        menuPopoverController = nil;
    }
}

@end
